package com.xinli.business.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinli.business.domain.TQuestion;
import com.xinli.business.domain.TQuestionFollow;
import com.xinli.business.mapper.TQuestionFollowMapper;
import com.xinli.business.service.ITQuestionFollowService;
import com.xinli.common.utils.DateUtils;
import com.xinli.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 问题关注Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-17
 */
@Service
public class TQuestionFollowServiceImpl extends ServiceImpl<TQuestionFollowMapper, TQuestionFollow>  implements ITQuestionFollowService
{
    @Autowired
    private TQuestionFollowMapper tQuestionFollowMapper;

    /**
     * 查询问题关注
     *
     * @param id 问题关注主键
     * @return 问题关注
     */
    @Override
    public TQuestionFollow selectTQuestionFollowById(String id)
    {
        return tQuestionFollowMapper.selectTQuestionFollowById(id);
    }

    /**
     * 查询问题关注列表
     *
     * @param tQuestionFollow 问题关注
     * @return 问题关注
     */
    @Override
    public List<TQuestionFollow> selectTQuestionFollowList(TQuestionFollow tQuestionFollow)
    {
        return tQuestionFollowMapper.selectTQuestionFollowList(tQuestionFollow);
    }

    /**
     * 新增问题关注
     *
     * @param tQuestionFollow 问题关注
     * @return 结果
     */
    @Override
    public int insertTQuestionFollow(TQuestionFollow tQuestionFollow)
    {
                tQuestionFollow.setCreateTime(DateUtils.getNowDate());
            return tQuestionFollowMapper.insertTQuestionFollow(tQuestionFollow);
    }

    /**
     * 修改问题关注
     *
     * @param tQuestionFollow 问题关注
     * @return 结果
     */
    @Override
    public int updateTQuestionFollow(TQuestionFollow tQuestionFollow)
    {
                tQuestionFollow.setUpdateTime(DateUtils.getNowDate());
        return tQuestionFollowMapper.updateTQuestionFollow(tQuestionFollow);
    }

    /**
     * 批量删除问题关注
     *
     * @param ids 需要删除的问题关注主键
     * @return 结果
     */
    @Override
    public int deleteTQuestionFollowByIds(String[] ids)
    {
        return tQuestionFollowMapper.deleteTQuestionFollowByIds(ids);
    }

    /**
     * 删除问题关注信息
     *
     * @param id 问题关注主键
     * @return 结果
     */
    @Override
    public int deleteTQuestionFollowById(String id)
    {
        return tQuestionFollowMapper.deleteTQuestionFollowById(id);
    }




    @Override
  public  Long  getFollowCount(String  businessId) {
      QueryWrapper<TQuestionFollow> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("business_id",businessId);
      long count = this.count(queryWrapper);
      return count;
  }

    @Override
    public  Boolean  isFollow(String  businessId) {
        QueryWrapper<TQuestionFollow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id",businessId);
        queryWrapper.eq("user_id",SecurityUtils.getUserIdStr());
        long count = this.count(queryWrapper);
        if(count>0){
            return true;
        }else{
            return false;
        }
    }
    @Override
    public Boolean followQuestion(String businessId) {
        // 获取当前用户ID（假设从安全上下文中获取）
        String userId = SecurityUtils.getUserIdStr();
        // 检查是否已关注
        QueryWrapper<TQuestionFollow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id", businessId)
                .eq("user_id", userId);
         List<TQuestionFollow>   existingFollow = this.list(queryWrapper);

        if (CollectionUtil.isEmpty(existingFollow)) {
            // 未关注，新增关注记录
            TQuestionFollow newFollow = new TQuestionFollow();
            newFollow.setBusinessId(businessId);
            newFollow.setUserId(userId);
            newFollow.setCreateTime(DateUtils.getNowDate());
            this.save(newFollow);
            return  true;
        } else {
            // 已关注，取消关注（删除记录）
            this.remove(queryWrapper);
            return  false;
        }
    }



}
